package main

import (
	"context"
	amqp "github.com/rabbitmq/amqp091-go"
	"time"
	"zhongxc.cc/go-common/errs"
	"zhongxc.cc/go-common/mq"
)

func Send() {
	conn := mq.NewRabbitMqConnection()
	channel, err := conn.Channel()
	errs.FailOnError(err, "Failed to open a channel")
	defer func(conn *amqp.Connection) {
		_ = conn.Close()
	}(conn)
	defer func(channel *amqp.Channel) {
		_ = channel.Close()
	}(channel)
	queue, err := channel.QueueDeclare(
		"hello",
		false,
		false,
		false,
		false,
		nil,
	)
	errs.FailOnError(err, "Failed to declare a queue")

	ctx, cancelFunc := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancelFunc()

	body := "hello world"
	err = channel.PublishWithContext(ctx,
		"",
		queue.Name,
		false,
		false,
		amqp.Publishing{
			ContentType: "text/plain",
			Body:        []byte(body),
		},
	)
	errs.FailOnError(err, "Failed to publish a message")
}

func main() {
	Send()
}
